﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;

namespace TaobaoDistrSales
{
    public partial class OptionsForm : DockContent
    {
        #region ctor and load

        private OptionsConfig config;

        public OptionsForm()
        {
            InitializeComponent();
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
            InitData();
            BindData();
        }

        #endregion

        #region Init

        protected void InitData()
        {
            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                config = (from c in entities.OptionsConfig
                          select c).FirstOrDefault();
                if (config == null)
                {
                    config = new OptionsConfig();
                    config.displaypanelnum = 1;
                    config.enablefont = true;
                    config.openpanelatstart = true;
                    config.pagesize = 20;
                    config.processinterval = 10;
                    config.showmax = true;
                    config.showlog = true;
                    config.clearoutput = true;
                    config.traderatecount = 1000;
                    config.updateDetailOrder = false;
                    config.updateNotFinishedTrade = true;
                    config.updatePurchaseOrder = true;
                    config.updateRefund = true;
                    config.updateTradeRate = true;
                    config.downloadTrade = true;
                    config.updatePurchaseOrderGetBeforeDays = 25;
                    entities.AddToOptionsConfig(config);
                    entities.SaveChanges();
                }
            }
        }

        protected void BindData()
        {
            PageSizeComboBox.DataSource = new int[] {10,20,50};             
            PageSizeComboBox.SelectedItem = config.pagesize;

            DashBoardCountComboBox.DataSource = new int[] {1};
            DashBoardCountComboBox.SelectedItem = config.displaypanelnum;

            IntervalComboBox.DataSource = new int[] { 1,5,10,30,60 };
            IntervalComboBox.SelectedItem = config.processinterval;

            EnableFormatCheckBox.Checked = config.enablefont;
            StartOpenCheckBox.Checked = config.openpanelatstart;
            ShowLogCheckBox.Checked = config.showlog;
            ShowMaxCheckBox.Checked = config.showmax;
            ClearOutputCheckBox.Checked = config.clearoutput;
            TradeRateCountText.Text = config.traderatecount.ToString();

            UpdateDetailCheckBox.Checked = config.updateDetailOrder;
            UpdateNotFinishedTradeCheckBox.Checked = config.updateNotFinishedTrade;
            UpdatePurchaseOrderCheckBox.Checked = config.updatePurchaseOrder;
            UpdateRefundCheckBox.Checked = config.updateRefund;
            UpdateTradeRateCheckBox.Checked = config.updateTradeRate;
            DownloadTradeCheckBox.Checked = config.downloadTrade;
            UpdatePurchaseOrderGetBeforeDaysTextBox.Text = config.updatePurchaseOrderGetBeforeDays.ToString();
        }

        #endregion

        private void OKButton_Click(object sender, EventArgs e)
        {
            int tradeRateCount = 1000;
            if (String.IsNullOrEmpty(TradeRateCountText.Text) || !Int32.TryParse(TradeRateCountText.Text, out tradeRateCount))
            {
                MessageBox.Show("每次更新评价数量 输入数据不合法！");
                return;
            }
            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                config = (from c in entities.OptionsConfig
                          where c.id == config.id
                          select c
                          ).FirstOrDefault();

                if (config != null)
                {
                    config.traderatecount = Convert.ToInt32(TradeRateCountText.Text);
                    config.openpanelatstart = StartOpenCheckBox.Checked;
                    config.displaypanelnum = (int)DashBoardCountComboBox.SelectedItem;
                    config.enablefont = EnableFormatCheckBox.Checked;
                    config.pagesize = (int)PageSizeComboBox.SelectedItem;
                    config.processinterval = (int)IntervalComboBox.SelectedItem;
                    config.showmax = ShowMaxCheckBox.Checked;
                    config.showlog = ShowLogCheckBox.Checked;
                    config.clearoutput = ClearOutputCheckBox.Checked;

                    config.updateDetailOrder = UpdateDetailCheckBox.Checked;
                    config.updateNotFinishedTrade = UpdateNotFinishedTradeCheckBox.Checked;
                    config.updatePurchaseOrder = UpdatePurchaseOrderCheckBox.Checked; 
                    config.updateRefund = UpdateRefundCheckBox.Checked ;
                    config.updateTradeRate = UpdateTradeRateCheckBox.Checked ;
                    config.downloadTrade = DownloadTradeCheckBox.Checked;
                    config.updatePurchaseOrderGetBeforeDays = Convert.ToInt32(UpdatePurchaseOrderGetBeforeDaysTextBox.Text.Trim());
                    //config.updatePurchaseOrderGetBeforeDays = config.updatePurchaseOrderGetBeforeDays > 10 ? config.updatePurchaseOrderGetBeforeDays : 10;
                    entities.SaveChanges();
                }

                ResourceManager.Current.OptionsConfig = config;
            }
            this.Close();
        }

        private void CLoseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
